package com.lianniu.web.tea.pay.service.impl;

import java.math.BigDecimal;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.order.dao.TeaOrderDao;
import com.lianniu.web.tea.order.model.TeaOrder;
import com.lianniu.web.tea.pay.service.ITeaPayService;
import com.lianniu.web.tea.product.dao.TeaProductDao;
import com.lianniu.web.tea.product.model.TeaProduct;
import com.lianniu.web.tea.supply.dao.TeaSupplyProductDao;
import com.lianniu.web.tea.supply.model.TeaSupplyProduct;
import com.lianniu.web.tea.user.info.dao.TeaFrontusersDao;
import com.lianniu.web.tea.user.info.model.TeaFrontusers;
import com.lianniu.web.util.MD5Util;
import com.lianniu.web.util.TeaContant;

@Service
@Transactional
public class TeaPayServiceImpl implements ITeaPayService {
	
	private final static Logger logger = Logger.getLogger(TeaPayServiceImpl.class);

	@Autowired
	private TeaFrontusersDao teaFrontusersDao;
	
	@Autowired
	private TeaOrderDao teaOrderDao;
	
	@Autowired
	private TeaSupplyProductDao teaProductDao;
	
	@Override
	public BaseResult balancePay(String orderId, String userId) {
		logger.info("TeaPayServiceImpl balancePay start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		TeaOrder teaOrder = teaOrderDao.selectByPrimaryKey(orderId);
		if(!"1".equals(teaOrder.getStatus())){
			logger.info("TeaPayServiceImpl balancePay end");
			return BaseResult.success(TeaContant.TEA_PAY_CHECK_RESULT_FALSE);
		}
		if(frontusers.getBalance().compareTo(teaOrder.getTotalunit())>=0){
			//商品销量增加
			String productid = teaOrder.getProductid();
			TeaSupplyProduct product = teaProductDao.selectByPrimaryKey(Long.valueOf(productid));
			Long sellNum = product.getTeaSellNum();
			if(sellNum==null){
				sellNum = Long.valueOf("0");
			}
			product.setTeaSellNum(sellNum+teaOrder.getConut());
			teaProductDao.updateByPrimaryKeySelective(product);
			
			frontusers.setBalance(frontusers.getBalance().subtract(teaOrder.getTotalunit())); //更改用户金额
			teaOrder.setStatus(TeaContant.TEA_ORDER_STATUS_PAY);
			teaOrder.setPaystyle(TeaContant.TEA_PAY_STYLE_BALANCE);
			teaFrontusersDao.updateByPrimaryKeySelective(frontusers);
			teaOrderDao.updateByPrimaryKeySelective(teaOrder);
			logger.info("TeaPayServiceImpl balancePay end");
			return BaseResult.success(TeaContant.TEA_PAY_CHECK_RESULT_TRUE);
		}else{
			logger.info("TeaPayServiceImpl balancePay end");
			return BaseResult.success(TeaContant.TEA_PAY_CHECK_RESULT_FALSE);
		}
		
	}

	@Override
	public BaseResult checkBalancePay(String orderId, String userId) {
		logger.info("TeaPayServiceImpl checkBalancePay start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		TeaOrder teaOrder = teaOrderDao.selectByPrimaryKey(orderId);
		boolean checkResult = false;
		if(frontusers.getBalance()==null){
			frontusers.setBalance(new BigDecimal("0"));
			teaFrontusersDao.updateByPrimaryKeySelective(frontusers);
		}
		if(frontusers.getBalance().compareTo(teaOrder.getTotalunit())>=0){
			checkResult = TeaContant.TEA_PAY_CHECK_RESULT_TRUE;
		}else{
			checkResult = TeaContant.TEA_PAY_CHECK_RESULT_FALSE;
		}
		logger.info("TeaPayServiceImpl checkBalancePay end");
		return BaseResult.success(checkResult);
	}

	@Override
	public BaseResult checkPayPassword(String payPassword, String userId) {
		logger.info("TeaPayServiceImpl checkPayPassword start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		if(MD5Util.checkPassword(payPassword, frontusers.getLoginpassword())){
			//密码正确
			logger.info("TeaPayServiceImpl checkPayPassword end");
			return BaseResult.success(TeaContant.TEA_PAY_PASSWORD_TRUE);
		}else{
			logger.info("TeaPayServiceImpl checkPayPassword end");
			return BaseResult.success(TeaContant.TEA_PAY_PASSWORD_FALSE);
		}
		
	}

	@Override
	public BaseResult getBalance(String userId) {
		logger.info("TeaPayServiceImpl getBalance start");
		TeaFrontusers frontusers = teaFrontusersDao.selectByPrimaryKey(Long.valueOf(userId));
		BigDecimal checkResult = new BigDecimal("0");
		if(frontusers.getBalance()==null){
		}else{
			checkResult =  frontusers.getBalance();
		}
		logger.info("TeaPayServiceImpl checkBalancePay end");
		return BaseResult.success(checkResult);
	}

	
}
